package cn.wolfcode.luowowo.website.controller;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.domain.Scenic;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import cn.wolfcode.luowowo.article.service.IScenicService;
import cn.wolfcode.luowowo.comment.domain.ScenicComment;
import cn.wolfcode.luowowo.comment.query.ScenicQueryObject;
import cn.wolfcode.luowowo.comment.service.IScenicCommentService;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.website.annotation.LoginUser;
import cn.wolfcode.luowowo.website.annotation.RequiredLogin;
import cn.wolfcode.luowowo.website.util.UploadUtil;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * Created by Administrator on 2019/11/22 0022.
 */
@Controller
@RequestMapping("/scenic")
public class ScenicController {
    @Value("${file.dir}")
    private String fileDir;
    @Reference
    private IScenicService scenicService;
    @Reference
    private IDestinationService destinationService;
    @Reference
    private IScenicCommentService scenicCommentService;


    //访问目的地主页,查询目的地,跳转
    @RequestMapping("/list")
    public String list(@ModelAttribute("qo") ScenicQueryObject qo, Model model) {
        //查询该目的地前5的景点
        List<Scenic> scenics = scenicService.selectByTOP5(qo.getDestId());
        model.addAttribute("eneity", scenics);
        //查询热门景点
        List<Scenic> hotScenics = scenicService.selectByHot();
        model.addAttribute("hotScenics", hotScenics);
        return "/scenic/list";
    }

    @RequestMapping("/type")
    public String type(@ModelAttribute("qo") ScenicQueryObject qo, Model model) {
        //根据type查询全部景点
        PageInfo pageInfo = scenicService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        return "/scenic/scenicPageTpl";
    }

    //显示detail页面
    @RequestMapping("/{id}")
    public String scenicId(@ModelAttribute("qo") ScenicQueryObject qo, @PathVariable("id") Long id, Model model) {
        Scenic scenic = scenicService.selectByPrimaryKey(id);
        //吐司
        List<Destination> toasts = destinationService.toastsList(scenic.getDestId());
        if (toasts != null && toasts.size() > 0) {
            Destination destination = toasts.remove(toasts.size() - 1);
            model.addAttribute("toasts", toasts);
            model.addAttribute("dest", destination);
        }


        model.addAttribute("scenic", scenic);
        return "/scenic/detail";
    }

    //显示评论页面commentTpl
    @RequestMapping("/commentTpl")
    //上传封面
    public String commentTpl(ScenicQueryObject qo, Model model) {
        //查询评论列表
        Page page = scenicCommentService.findByScenicId(qo);
        model.addAttribute("page", page);
        return "/scenic/commentTpl";
    }

    //上传图片
    @RequestMapping("/coverImageUpload")
    @ResponseBody
    //上传封面
    public String input(MultipartFile pic) {
        //上传封面操作
        return UploadUtil.upload(pic, fileDir);
    }

    //添加评论CommentSave
    @RequiredLogin
    @RequestMapping("/saveComment")
    @ResponseBody
    public Object saveComment(ScenicComment scenicComment, @LoginUser UserInfo userInfo) {
        AjaxResult result = new AjaxResult();
        //设置用户信息
        try {
            scenicComment.setUserId(userInfo.getId());
            scenicComment.setUsername(userInfo.getNickname());
            scenicComment.setLevel(userInfo.getLevel());
            scenicComment.setHeadUrl(userInfo.getHeadImgUrl());
            //保存评论
            scenicCommentService.save(scenicComment);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }

    //点赞scenicThumbup
    @RequiredLogin
    @RequestMapping("/scenicThumbup")
    @ResponseBody
    public Object scenicThumbup(String id, @LoginUser UserInfo userInfo) {
        AjaxResult result = scenicCommentService.scenicThumbup(id, userInfo.getId());
        return result;
    }

}
